<?php
	global $debug_pre_content, $trigger_uri, $document_root;
	
	// Get all images from directory for this content site
	$string = "";
	$fileCount = 0;
	$filePath = dirname(__FILE__)."/../images/".$trigger_uri."/";
	$artwork_folder = false;
	$artcontainer_width = "";
	
	$debug_pre_content[] = "Path to look for images in: $filePath";
	
	//Check if the folder should be treated as artwork folder
	if (file_exists($filePath."/artwork/")) {
		$debug_pre_content[] = "Artwork folder detected in  ".$filePath."... Saving images for single page view.";
		$artwork_folder = true;
		$filePath = $filePath."artwork/";
	} else {
		$debug_pre_content[] = "Normal image folder detected in ".$filePath."... Continuing with image list.";
	}
	
	// Specify the path you want to look in
	$dir = opendir($filePath);
	
	// Save image filenames to image array
	$image_list = array();
	
	while ($file = readdir($dir)) {
		if (preg_match("/(\.png|\.gif|\.jpg)/", $file)) {// Look at only image files
			$image_list[] = $file;
		}
	}
	
	// Sort the filelist alphabetically
	sort($image_list);
	
	// If the image folder is NOT an artwork folder (normal image folder)
	if (!$artwork_folder) {
		
		// Set the width of the artwork container
		// @todo make this dynamic
		$artcontainer_width = "style='min-width: 950px;'";
		// Set image which is displayed
		$display_image = (isset($_GET['image'])) ? urldecode($_GET['image']) : $image_list[0] ;
		$debug_pre_content[] = "Display name is: $display_image";
		
		// Thumbs list
		$thumb_list = "";
		
		// If imagelist is above one, show thumblist
		if (count($image_list) > 1) {
			foreach ($image_list as $number => $filename) {
				// Leave out thumb from active image
				if (!file_exists($filePath."thumbs/".$filename)) {
					$debug_pre_content[] = "No file at: \"".$document_root."/images/".$trigger_uri."/thumbs/".$filename."\" Skipping loop once.";
					continue;
				}
				
				$thumb_list .= "<li title='".$filename."'><a class='thumb_link' href='?image=".urlencode($filename)."'><img src='$document_root/images/$trigger_uri/thumbs/$filename' /></a></li>";
			}
		}
		
		// Get caption list
		if (file_exists($filePath.'caption.xml')) {
			$xml = simplexml_load_file($filePath.'caption.xml');
			
			$image_title	= $xml->picture[0]->title;
			$image_caption	= $xml->picture[0]->caption;
			for ($i = 0 ; $i < count($xml->picture); $i++) {
				if ($xml->picture[$i]->filename == $_GET['image']) {
					$image_title	= $xml->picture[$i]->title;
					$image_caption	= $xml->picture[$i]->caption;
				}
			}
		}
		
		// Current image uri
		$current_image_uri = $document_root."/images/$trigger_uri/".$display_image;
		// Current image size
		$current_image_size = getimagesize($document_root."/images/$trigger_uri/".$display_image);
		
		// Set image display variables
		// Set image max size
		$max_image_width	= 950;
		$max_image_height	= 670;
		if ($current_image_size[0] > $max_image_width || $current_image_size[1] > $max_image_height) {
			$image_ratio		= MIN( $max_image_width / $current_image_size[0], $max_image_height / $current_image_size[1] );
			$new_width			= $image_ratio * $current_image_size[0];
			$new_height			= $image_ratio * $current_image_size[1];
		} else {
			$new_width			= $current_image_size[0];
			$new_height			= $current_image_size[1];
		}
		
		$image_content = "
			<img class='single_active' src='$current_image_uri' width='$new_width' height='$new_height' />
			<div id='information' style='width: ".$new_width."px; display: none;'>
				<h2 id='image_title'>$image_title</h2>
				<p id='image_caption'>$image_caption</p>
			</div>
		";
	} else {
		$artcontainer_width = "style='width: 950px;'";

		// display the artwork as a whole on the site
		$stage_width = 950;
		$image_margin = 5;
		$image_count = count($image_list);
		$marge_me = "marge_me";
		$artwork_output = "";
		
		$image_width = round(($stage_width - ($image_margin * ($image_count - 1))) / $image_count);
		
		for ($i=0; $i < $image_count; $i++) {
			$current_image_uri = $document_root."/images/$trigger_uri/artwork/".$image_list[$i];
			$this_size = getimagesize($current_image_uri);
			
			$this_width = $this_size[0];
			$this_height = $this_size[1];
			
			// get ratio
			$ratio = $image_width / $this_width;
			
			// calculate new height
			$image_height = round($this_height * $ratio);
			
			$marge_me = ($i == $image_count - 1) ? "" : $marge_me ;
			$artwork_output .= "<img class='single_active $marge_me' src='$current_image_uri' width='$image_width' height='$image_height' />";
		}
		$image_content = $artwork_output;
	}
	
	$filepath2 = dirname(__FILE__)."/";
	// Fetch the genre desc for the site
	if (file_exists($filepath2."genre_desc/".$trigger_uri."_desc.php")) {
		$genre_desc = file_get_contents($filepath2."genre_desc/".$trigger_uri."_desc.php");
	} else {
		$genre_desc = "";
	}
	
	// Append genre desc to content
	$image_content .= $genre_desc;
?>
<div id="main" role="main" class="<?php echo $trigger_uri; ?>">
	<div id="art_container" <?php echo $artcontainer_width; ?>>
		<div id="blur_cover"></div>
		<div id="information_button"><span>Information</span></div>
		<?php echo $image_content; ?>
		<?php if ($thumb_list != "") { ?>
		<div id="sidebar">
			<ul>
				<?php echo $thumb_list; ?>
			</ul>
		</div>
	<?php } ?>
	</div>
	
</div>
<footer>

</footer>